package com.uniinnovate.lanjingling.controller;

import java.util.Arrays;
import java.util.Map;

import com.uniinnovate.lanjingling.entity.ResumeEntity;
import com.uniinnovate.lanjingling.pojo.vo.JobApplicationVO;
import com.uniinnovate.lanjingling.pojo.vo.UserInfoVO;
import com.uniinnovate.lanjingling.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.uniinnovate.lanjingling.entity.UserEntity;
import com.uniinnovate.lanjingling.utils.PageUtils;
import com.uniinnovate.lanjingling.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * @author LianChuangTech
 * @email LianChuangTech@163.com
 */
@Api(tags = "个人用户相关")
@RestController
@RequestMapping("/lanjingling/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OssService ossService;

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private JobService jobService;

    @Autowired
    private JobApplicationService jobApplicationService;

    /**
     * 列表
     */
    @ApiOperation("查看用户列表")
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("查看用户信息")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @ApiOperation("保存用户信息")
    @PostMapping("/save")
    public R save(@RequestBody UserEntity user) {
        userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改用户信息")
    @PutMapping("/update")
    public R update(@RequestBody UserEntity user) {
        userService.updateById(user);

        return R.ok();
    }

    /**
     * 批量删除
     */
    @ApiOperation("批量删除用户信息")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 指定删除
     */
    @ApiOperation("指定删除用户信息")
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable("id") Long id) {
        userService.removeById(id);

        return R.ok();
    }

    /**
     * 上传简历
     */
    @ApiOperation("上传简历")
    @PostMapping("/uploadResume")
    public R uploadResume(@RequestParam("file") MultipartFile file,
                          @RequestParam("userId") Long userId, HttpServletRequest request) {
        String url = ossService.uploadFile(file);
        ResumeEntity resumeEntity = new ResumeEntity();
        resumeEntity.setUserId(userId);
        resumeEntity.setUrl(url);
        resumeService.save(resumeEntity);
        return R.ok().put("url", url);
    }

    /**
     * 搜索职位
     */
    @ApiOperation("搜索职位")
    @GetMapping("/searchJob")
    public R searchJob(@RequestParam("word") String word) {
        return jobService.searchJob(word);
    }

    /**
     * 提交申请
     */
    @ApiOperation("提交申请")
    @PostMapping("/submitApply")
    public R submitApply(@RequestBody JobApplicationVO jobApplicationVO) {
        return jobApplicationService.submitApply(jobApplicationVO);
    }

    /**
     * 完善个人信息
     */
    @ApiOperation("完善个人信息")
    @PostMapping("/perfectInfo")
    public R perfectInfo(@RequestBody UserInfoVO userInfoVO) {
        return userService.perfectInfo(userInfoVO);
    }

    /**
     * 查看消息
     */
    @ApiOperation("查看消息")
    @GetMapping("/browseMsg")
    public R browseMsg(@RequestParam("userId") Long userId) {

        return R.ok();
    }
}
